package de.tum.in.far.threedui.superbrickball.engine.physics;

import javax.vecmath.Tuple3f;

import de.tum.in.far.threedui.superbrickball.engine.physics.models.PhysicalModel;
import de.tum.in.far.threedui.superbrickball.engine.physics.models.PlaneModel;
import de.tum.in.far.threedui.superbrickball.engine.physics.models.SphereModel;

public class CollisionDetector {
	public static float distance(Tuple3f t1, Tuple3f t2)
	{
		return (float) Math.sqrt(Math.pow(t1.x - t2.x, 2) + Math.pow(t1.y - t2.y, 2) + Math.pow(t1.z - t2.z, 2));
	}
	
	public static boolean isColliding(SphereModel s1, PhysicalModel pm, float error)
	{
		if(pm instanceof SphereModel)
		{
			return isColliding(s1, (SphereModel) pm, error);
		}
		else if(pm instanceof PlaneModel)
		{
			return isColliding(s1, (PlaneModel) pm, error);
		}
		
		return false;
	}
	
	
	public static boolean isColliding(SphereModel s1, SphereModel s2, float error)
	{
		return distance(s1.getPosition(), s2.getPosition()) <= s1.getRadius() + s2.getRadius() + error;
	}
	
	public static boolean isColliding(SphereModel sphere, PlaneModel plane, float error)
	{
//		System.out.printf("COLLIDING :[%s], RADIUS :[%f], POS :[%s], DISTANCE :[%f]\n",plane.distance(sphere.getPosition()) <= sphere.getRadius(), sphere.getRadius(), sphere.getPosition(), plane.distance(sphere.getPosition()));
		return plane.distance(sphere.getPosition()) <= sphere.getRadius() + error;
	}

}
